package com.itsu.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itsu.dao.QuestionDao;
import com.itsu.domain.Question;
import com.itsu.service.QuestionService;
import com.itsu.utils.MapperUtils;

import javax.servlet.ServletOutputStream;
import java.util.List;

public class QuestionServiceImpl implements QuestionService {

    private QuestionDao mapper;

    @Override
    public void save(Question question) {
        try {
            mapper = MapperUtils.getMapper(QuestionDao.class);
            mapper.save(question);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public void delete(Question question) {
        try {
            mapper = MapperUtils.getMapper(QuestionDao.class);
            mapper.delete(question);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public void update(Question question) {
        try {
            mapper = MapperUtils.getMapper(QuestionDao.class);
            mapper.update(question);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public Question findById(String id) {
        try {
            mapper = MapperUtils.getMapper(QuestionDao.class);
            Question question = mapper.findById(id);
            return question;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public List<Question> findAll() {
        try {
            mapper = MapperUtils.getMapper(QuestionDao.class);
            List<Question> questionList = mapper.findAll();
            return questionList;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public PageInfo<Question> findAll(int pageNum, int pageSize) {
        try {
            mapper = MapperUtils.getMapper(QuestionDao.class);
            PageHelper.startPage(pageNum, pageSize);
            List<Question> all = mapper.findAll();
            PageInfo<Question> questionPageInfo = new PageInfo<>(all);
            return questionPageInfo;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public void downLoadExcel(ServletOutputStream outputStream, String realPath) {
        try {
            mapper = MapperUtils.getMapper(QuestionDao.class);
            List<Question> all = mapper.findAll();
            EasyExcel.write(outputStream, Question.class)
                    .withTemplate(realPath)
                    .sheet()
                    .doFill(all);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }
}
